package com.demostic.demostic_test.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.util.Iterator;
import java.util.List;

public class WifiUtils {
    public static final String TAG = WifiUtils.class.getSimpleName();
    public static final int WIFICIPHER_NOPASS = 1;
    public static final int WIFICIPHER_WEP = 3;
    public static final int WIFICIPHER_WPA = 2;

    public static List<ScanResult> getWifiList(Context context) {
        return ((WifiManager) context.getSystemService("wifi")).getScanResults();
    }

    public static WifiConfiguration isExist(WifiManager wifiManager, String ssid) {
        List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
        if (configs != null && configs.size() > 0) {
            for (WifiConfiguration config : configs) {
                if (config.SSID.equals("\"" + ssid + "\"")) {
                    return config;
                }
            }
        }
        return null;
    }

    public static void connectToAp_(WifiManager wifiManager, String ssid, String pass, int type) {
        createWifiConfig(wifiManager, ssid, pass, type);
    }

    public static void createWifiConfig(WifiManager mWifiManager, String ssid, String password, int type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.SSID = "\"" + ssid + "\"";
        WifiConfiguration tempConfig = isExist(mWifiManager, ssid);
        if (tempConfig != null) {
            Log.e(TAG, "createWifiConfig: " + mWifiManager.enableNetwork(tempConfig.networkId, true));
            return;
        }
        if (type == 1) {
            config.allowedKeyManagement.set(0);
        } else if (type == 3) {
            config.hiddenSSID = true;
            config.wepKeys[0] = "\"" + password + "\"";
            config.allowedAuthAlgorithms.set(0);
            config.allowedAuthAlgorithms.set(1);
            config.allowedKeyManagement.set(0);
            config.wepTxKeyIndex = 0;
        } else if (type == 2) {
            config.preSharedKey = "\"" + password + "\"";
            config.hiddenSSID = true;
            config.allowedAuthAlgorithms.set(0);
            config.allowedGroupCiphers.set(2);
            config.allowedKeyManagement.set(1);
            config.allowedPairwiseCiphers.set(1);
            config.allowedGroupCiphers.set(3);
            config.allowedPairwiseCiphers.set(2);
            config.status = 2;
        }
        int netId = mWifiManager.addNetwork(config);
        mWifiManager.disconnect();
        Log.e(TAG, "createWifiConfig: " + mWifiManager.enableNetwork(netId, true));
    }

    public static String getSsid(Context context) {
        String ssid = "";
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService("wifi");
        if (wifiManager != null) {
            int networkId = wifiManager.getConnectionInfo().getNetworkId();
            Iterator<WifiConfiguration> it = wifiManager.getConfiguredNetworks().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                WifiConfiguration wifiConfiguration = it.next();
                if (wifiConfiguration.networkId == networkId) {
                    ssid = wifiConfiguration.SSID;
                    break;
                }
            }
        }
        if (ssid.contains("\"")) {
            return ssid.replace("\"", "");
        }
        return ssid;
    }

    public static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService("connectivity");
        if (cm == null) {
            return false;
        }
        NetworkInfo wifiInfo = cm.getNetworkInfo(1);
        return wifiInfo.isConnected() && wifiInfo.isAvailable();
    }

    public static boolean isWifiConnected_() {
        return true;
    }
}
